package com.ibm.commerce.url.tasks.accelerator;

/**
 * Info 1
 * Info 2
 * Info 3
 * Info 4
 * Info 5
 * Info 6
 */

import com.ibm.commerce.url.harness.*;
import com.ibm.commerce.url.util.ContentSearcher;
import com.ibm.commerce.url.util.Log;
import com.ibm.commerce.url.exception.*;
import com.ibm.wc.test.exception.DatablockMissingException;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

public class AcceleratorUploadAttachment extends URLTestTask {
	/********************************************************
	 * Section - Input Parameter names constants
	 * 
	 * If tester doesn't know what parameters a task has he can use dot method to refer to a parameter easily, otherwise
	 * he has to use String to pass the parameter name and it's prone to error. For example:
	 * 
	 * 	String currentFulfilmentCenterID = browserContext.getOutputParam(
	 * 		TaskName.INPUT_PARAMETER_NAME_FULFILLMENTCENTERID
	 * 	);
	 * 
	 * These lines are automatically generated from task definition XML file by code generator.
	 *  
	 ********************************************************/
	public static final String INPUT_PARAMETER_NAME_STOREID = "storeId"; 
	public static final String INPUT_PARAMETER_NAME_LANGID = "langId";
	public static final String INPUT_PARAMETER_NAME_FULFILLMENTCENTERID = "fulfillmentCenterId";
	public static final String INPUT_PARAMETER_NAME_SEARCHCRITERIA = "searchCriteria";
	
	private static AcceleratorUploadAttachment instance = new AcceleratorUploadAttachment();
	private AcceleratorUploadAttachment(){}
	
	public static void execute() throws TaskFailException, DatablockMissingException {
		instance.runTask();
	}
	public static void execute(int counterIndex) throws TaskFailException, DatablockMissingException {
		instance.runTask(counterIndex);
	}
	
	protected void executeTask() throws TaskFailException {
		acceleratorUploadAttachmentStep1();
		acceleratorUploadAttachmentStep2();
		acceleratorUploadAttachmentStep3();		
	}

	private void acceleratorUploadAttachmentStep1() throws TaskFailException {
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorUploadAttachmentStep1");
		
		//LaunchCategoriesPage
		WebHit("https://standardhost/webapp/wcs/tools/servlet/CategoryTreeView", WEB_FORM_GET);
		
		Map<String, String> LaunchCategoriesPage_DYNAMIC001	= new LinkedHashMap<String, String>();
		LaunchCategoriesPage_DYNAMIC001.put("XMLFile", "catalog.catalogGroupTree");
		WebHit("https://standardhost/webapp/wcs/tools/servlet/DynamicTreeView", WEB_FORM_GET, LaunchCategoriesPage_DYNAMIC001);
		
		WebHit("https://standardhost/webapp/wcs/tools/servlet/CategoryTreeButtonView", WEB_FORM_GET);
		
		Map<String, String> LaunchCategoriesPage_DYNAMIC002	= new LinkedHashMap<String, String>();
		LaunchCategoriesPage_DYNAMIC002.put("XMLFile", "catalog.catalogGroupTree");
		WebHit("https://standardhost/webapp/wcs/tools/servlet/DynamicTreeFetchDataView", WEB_FORM_GET, LaunchCategoriesPage_DYNAMIC002);
	  
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorUploadAttachmentStep1");	
	}
	
	private static Map<String, String> LaunchAddAttachmentPage_PICKATT001 = new LinkedHashMap<String, String>();
	static{
		LaunchAddAttachmentPage_PICKATT001.put("objectId","");
		LaunchAddAttachmentPage_PICKATT001.put("objectType","");		
		LaunchAddAttachmentPage_PICKATT001.put("saveChanges","true");
		LaunchAddAttachmentPage_PICKATT001.put("returnPage","1");
		
	}
	
	private static Map<String, String> LaunchAddAttachmentPage_DYNAMI002 = new LinkedHashMap<String, String>();
	static{
		LaunchAddAttachmentPage_DYNAMI002.put("XMLFile","attachment.AttachmentTree");		
	}
	
	
	private static Map<String, String> LaunchAddAttachmentPage_DYNAMIC003 = new LinkedHashMap<String, String>();
	static{
		LaunchAddAttachmentPage_DYNAMIC003.put("XMLFile","attachment.AttachmentTree");		
	}
	private static Map<String, String> LaunchAddAttachmentPage_NEWDYNA004 = new LinkedHashMap<String, String>();
	static{
		LaunchAddAttachmentPage_NEWDYNA004.put("directoryId","");	
		LaunchAddAttachmentPage_NEWDYNA004.put("tool","browser");	
		LaunchAddAttachmentPage_NEWDYNA004.put("ActionXMLFile","attachment.attachmentAssetsList");	
		LaunchAddAttachmentPage_NEWDYNA004.put("cmd","PickAttachmentAssetsToolResultList");	
	}
	private static Map<String, String> LaunchAddAttachmentPage_PICKATT006 = new LinkedHashMap<String, String>();
	static{
		LaunchAddAttachmentPage_PICKATT006.put("startindex","0");	
		LaunchAddAttachmentPage_PICKATT006.put("encoding","UTF-8");	
		LaunchAddAttachmentPage_PICKATT006.put("SubmitErrorMessage","");	
		LaunchAddAttachmentPage_PICKATT006.put("storeDir","no");	
		LaunchAddAttachmentPage_PICKATT006.put("tool","browser");	
		LaunchAddAttachmentPage_PICKATT006.put("orderby","OFFER.OFFER_ID");	
		LaunchAddAttachmentPage_PICKATT006.put("directoryId","");	
		LaunchAddAttachmentPage_PICKATT006.put("endindex","0");	
		LaunchAddAttachmentPage_PICKATT006.put("view","list");	
		LaunchAddAttachmentPage_PICKATT006.put("listsize","15");	
		LaunchAddAttachmentPage_PICKATT006.put("ActionXMLFile","attachment.attachmentAssetsList");	
		LaunchAddAttachmentPage_PICKATT006.put("SubmitFinishMessage","");	
		LaunchAddAttachmentPage_PICKATT006.put("productrfnbr","");	
		LaunchAddAttachmentPage_PICKATT006.put("viewTaskName","NewDynamicListView");	
	}
		
	
	static String directoryId = null;
	private void acceleratorUploadAttachmentStep2() throws TaskFailException{
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorUploadAttachmentStep2");
		putParameterInMap(LaunchAddAttachmentPage_PICKATT001, "objectId", "51200000001");
		  putParameterInMap(LaunchAddAttachmentPage_PICKATT001, "objectType", "CATENTRY");
		  WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsTool", WEB_FORM_POST, LaunchAddAttachmentPage_PICKATT001);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolSelection", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet//DynamicTreeView",  WEB_FORM_GET, LaunchAddAttachmentPage_DYNAMI002);
		 	WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolSearchButtons", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolSearchForm", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolBrowseHidden", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolBrowseTitle", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolURLForm", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolActionButtons", WEB_FORM_GET);
			WebHit("https://standardhost/webapp/wcs/tools/servlet/DynamicTreeFetchDataView",  WEB_FORM_GET, LaunchAddAttachmentPage_DYNAMIC003); 
			
			directoryId = strSearchReg("TreeNodeValueObj.\\\\'(\\d*)\\\\'", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
	   putParameterInMap(LaunchAddAttachmentPage_NEWDYNA004, "directoryId", directoryId);
		  putParameterInMap(LaunchAddAttachmentPage_PICKATT006, "directoryId", directoryId);
		 WebHit("https://standardhost/webapp/wcs/tools/servlet/NewDynamicListView", WEB_FORM_POST, LaunchAddAttachmentPage_NEWDYNA004);
		 WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolResultList", WEB_FORM_POST, LaunchAddAttachmentPage_PICKATT006);
			
		 directoryId = strSearchReg("setBrowseResultListFrame.\"(\\d+)\".;", ContentSearcher.FLAG_EXTRACT_FROM_GROUP);
		 
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorUploadAttachmentStep2");
	}
	
	
	
	private static Map<String, String> UploadAttachment_NEWDYNA001 = new LinkedHashMap<String, String>();
	static{
		UploadAttachment_NEWDYNA001.put("directoryId","");		
		UploadAttachment_NEWDYNA001.put("tool","browser");	
		UploadAttachment_NEWDYNA001.put("ActionXMLFile","attachment.attachmentAssetsList");	
		UploadAttachment_NEWDYNA001.put("cmd","PickAttachmentAssetsToolResultList");	
	}
	private static Map<String, String> UploadAttachment_PICKATT003 = new LinkedHashMap<String, String>();
	static{
		UploadAttachment_PICKATT003.put("startindex","0");	
		UploadAttachment_PICKATT003.put("encoding","UTF-8");	
		UploadAttachment_PICKATT003.put("SubmitErrorMessage","");	
		UploadAttachment_PICKATT003.put("storeDir","no");	
		UploadAttachment_PICKATT003.put("tool","browser");	
		UploadAttachment_PICKATT003.put("orderby","OFFER.OFFER_ID");	
		UploadAttachment_PICKATT003.put("directoryId","");	
		UploadAttachment_PICKATT003.put("endindex","0");	
		UploadAttachment_PICKATT003.put("view","list");	
		UploadAttachment_PICKATT003.put("listsize","200");	
		UploadAttachment_PICKATT003.put("ActionXMLFile","attachment.attachmentAssetsList");	
		UploadAttachment_PICKATT003.put("SubmitFinishMessage","");	
		UploadAttachment_PICKATT003.put("productrfnbr","");	
		UploadAttachment_PICKATT003.put("viewTaskName","NewDynamicListView");			
	}
	
	private void acceleratorUploadAttachmentStep3() throws TaskFailException{	
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorUploadAttachmentStep3");
		
		HTTPMultipartEntity reqEntity = new HTTPMultipartEntity();

		try {
			reqEntity.addPart("url", new HTTPStringBody("PickAttachmentAssetsToolResult"));
			reqEntity.addPart("directory", new HTTPStringBody("Elite&#47;Attachment"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		HTTPFileBody fileBody = new HTTPFileBody(new File(
				"D:\\testupload.txt"), "text/plain");
		reqEntity.addPart("filename", fileBody);
		Log.finest("$$$$$$$"+ reqEntity.getContentLength());
		WebHit("https://standardhost/webapp/wcs/tools/servlet/AttachmentUpload?url=PickAttachmentAssetsToolResult&directory=/Elite&#47;Attachment", WEB_FORM_POST_BIN,reqEntity);
		
		putParameterInMap(UploadAttachment_NEWDYNA001, "directoryId", directoryId);
		putParameterInMap(UploadAttachment_PICKATT003, "directoryId", directoryId);
		      
		WebHit("https://standardhost/webapp/wcs/tools/servlet/NewDynamicListView", WEB_FORM_POST, UploadAttachment_NEWDYNA001);
		WebHit("https://standardhost/webapp/wcs/tools/servlet/PickAttachmentAssetsToolResultList", WEB_FORM_POST, UploadAttachment_PICKATT003);

		    
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorUploadAttachmentStep3");
	}
	
}
